package com.yxq.mdt.app.modules.socialcontact.ui.fragment.main;


import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.orhanobut.hawk.Hawk;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.base.BaseFragment;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.modules.socialcontact.adapter.BeinteretedAllListAdapter;
import com.yxq.mdt.app.modules.socialcontact.bean.AttentionManAndDynamicBean;
import com.yxq.mdt.app.modules.socialcontact.bean.SubscribersListBean;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialAttemtionPresenter;
import com.yxq.mdt.app.modules.socialcontact.presenter.MainSocialAttentionContract;
import com.yxq.mdt.app.net.ApiModel;

import butterknife.BindView;
import cn.jzvd.Jzvd;

/**
 * @author L
 * @time
 * @describe 可能感兴趣的人列表
 */
public class BeInterestedListFragment extends BaseFragment<MainSocialAttemtionPresenter> implements MainSocialAttentionContract.View {
    @BindView(R.id.nearby_recycler)
    RecyclerView nearbyRecycler;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private BeinteretedAllListAdapter nearbyDetailsAdapter;

    @Override
    protected int attachLayoutId() {
        return R.layout.fragment_nearbey_list_detalils;
    }

    @Override
    protected MainSocialAttemtionPresenter getPresenter() {
        return new MainSocialAttemtionPresenter(this, new MainSocialContactModel());
    }

    @Override
    public void initView(View view) {
        nearbyRecycler.setLayoutManager(new LinearLayoutManager(mContext));
        nearbyDetailsAdapter = new BeinteretedAllListAdapter(R.layout.item_nearby_details_list,null);
        nearbyRecycler.setAdapter(nearbyDetailsAdapter);
        nearbyDetailsAdapter.setOnItemChildClickListener((adapter, view1, position) -> {
            switch (view1.getId()) {
                case R.id.nearby_attention_tv:
                    TextView attention = view1.findViewById(R.id.nearby_attention_tv);
                    MainSocialContactModel.setInterestRelation(nearbyDetailsAdapter.getItem(position).getUserId() + "", 1, 1, new ApiModel.CommonCallback<String>() {
                        @Override
                        public void onSuccess(String s) {
                            attention.setVisibility(View.GONE);
                            showCenterToast("关注成功");
                        }

                        @Override
                        public void onError(String errMsg) {
                            showCenterToast("关注失败");
                        }
                    });
                    break;
            }
        });
        nearbyDetailsAdapter.setEmptyView(R.layout.layout_empty_view, nearbyRecycler);
        nearbyDetailsAdapter.isUseEmpty(false);
        refreshLayout.setOnRefreshListener(refreshLayout -> {
            page = 1;
            mPresenter.getMaybeKnowList(page, 20);
        });
        refreshLayout.setOnLoadMoreListener(refreshLayout -> {
            page++;
            mPresenter.getMaybeKnowList(page, 20);
        });
    }

    @Override
    public void initData() {
        refreshLayout.autoRefresh();
    }

    @Override
    public void setData(@Nullable Object data) {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Jzvd.releaseAllVideos();
    }

    @Override
    public void onLoadMaybeKnowSuccess(AttentionManAndDynamicBean attentionManAndDynamicBean) {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadMore();

        if (page == 1) {
            nearbyDetailsAdapter.setNewData(attentionManAndDynamicBean.getList());
        } else {
            if (attentionManAndDynamicBean.getList() != null && attentionManAndDynamicBean.getList().size() != 0) {
                nearbyDetailsAdapter.addData(attentionManAndDynamicBean.getList());
            } else {
                refreshLayout.finishLoadMoreWithNoMoreData();
            }
        }
        nearbyDetailsAdapter.isUseEmpty(!(nearbyDetailsAdapter.getData().size() > 0));
    }

    @Override
    public void onLoadMaybeKnowFailed() {

    }

    @Override
    public void onLoadSubscribersListSuccess(SubscribersListBean subscribersListBean) {

    }

    @Override
    public void onLoadSubscribersListFailed() {

    }
}
